package com.leihao.marketing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.leihao.common.core.exception.ServiceException;
import com.leihao.marketing.domain.Coupon;
import com.leihao.marketing.domain.CouponListRequest;
import com.leihao.marketing.mapper.CouponMapper;
import com.leihao.marketing.service.CouponService;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements CouponService {

    @Override
    public IPage<Coupon> queryList(CouponListRequest listRequest) {
        if (com.leihao.common.core.utils.StringUtils.isNull(listRequest)) {
            throw new ServiceException("参数不能为空");
        }
        Page<Coupon> couponPage = new Page<>(listRequest.getPageNum(), listRequest.getPageSize());
        QueryWrapper<Coupon> queryWrapper = Wrappers.query();
        //参数校验
        if (StringUtils.isNotEmpty(listRequest.getCouponName())) {
            queryWrapper.like("coupon_name", "%" + listRequest.getCouponName() + "%");
        }

        // 判断listRequest.getCouponType()的值
        if (listRequest.getCouponType() != null && listRequest.getCouponType() < 0) {
            throw new ServiceException("请选择正确的优惠券类型");
        }
        if (listRequest.getCouponType() != null){
            queryWrapper.eq("coupon_type", listRequest.getCouponType());
        }
        queryWrapper.eq("is_delete", 0);
        return this.page(couponPage, queryWrapper);
    }

    @Override
    public List<Coupon> findAll() {
        QueryWrapper<Coupon> queryWrapper = Wrappers.query();
        queryWrapper.eq("is_delete", 0);
        return this.list(queryWrapper);
    }
}
